﻿using System;
using System.Windows.Forms;
using Setup.Entities.AppFinder;
using Setup.Interfaces;

namespace Setup.Pages
{
    public partial class Dependencies : UserControl, ITabControl
    {
        public string DownloadDirectory { get; set; }

        public string RavenDbInstallationPath { get; set; }

        public Dependencies()
        {
            InitializeComponent();
        }

        private void Dependencies_Load(object sender, EventArgs e)
        {
        }

        public bool IsComplete
        {
            get
            {
                return appFinder1.IsComplete
                       && appFinder2.IsComplete
                       && appFinder3.IsComplete
                       && appFinder4.IsComplete;
            }
        }

        public void Start()
        {
            DownloadDirectory = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.DirectorySeparatorChar + "downloads" + System.IO.Path.DirectorySeparatorChar;

            appFinder1.ApplicationInfo = new AppInfo
            {
                ApplicationName = "7-zip (Will be used to unpack applications and media)",
                ApplicationInfoUrl = "http://www.7-zip.org/",
                ApplicationDownloadUrl = "http://downloads.sourceforge.net/sevenzip/7z920.exe",
                TemporaryFileName = DownloadDirectory + "7z.exe",
                ApplicationFileHash = "6GkOCJIYl2GLBcustTQNsOivMh3W7m4+",
                InstallationSearchPattern = "7z.exe"
            };

            appFinder2.ApplicationInfo = new AppInfo
            {
                ApplicationName = "Raven DB (Your data will be stored here)",
                ApplicationInfoUrl = "http://www.ravendb.net/",
                ApplicationDownloadUrl = "http://builds.hibernatingrhinos.com/download/2409", // https://builds.hibernatingrhinos.com/download/2409
                TemporaryFileName = DownloadDirectory + "ravendb.zip", // (Build-531)
                ApplicationFileHash = "0DpSPcVmvABTzOCC9ZmrguP/NccYaiGR",
                InstallationSearchPattern = "Raven.Server.exe"
            };

            appFinder3.ApplicationInfo = new AppInfo
            {
                ApplicationName = "VLC (Will be used to stream and convert media)",
                ApplicationInfoUrl = "http://www.videolan.org/vlc/",
                ApplicationDownloadUrl =
                    "http://downloads.sourceforge.net/project/vlc/1.1.11/win32/vlc-1.1.11-win32.exe?r=http%3A%2F%2Fwww.videolan.org%2Fvlc%2F&ts=1321650679&use_mirror=heanet",
                TemporaryFileName = DownloadDirectory + "vlc.exe",
                ApplicationFileHash = "WZ6ZuB3btKFrMCqKmTUVCO934+1Mv/25",
                InstallationSearchPattern = "vlc.exe"
            };

            appFinder4.ApplicationInfo = new AppInfo
            {
                ApplicationName = "FlowMedia Web Server",
                ApplicationInfoUrl = "http://code.google.com/p/flowmedia/",
                TemporaryFileName = AppDomain.CurrentDomain.BaseDirectory + System.IO.Path.DirectorySeparatorChar + "MediaStreamer.exe"
            };

            if (!System.IO.Directory.Exists(DownloadDirectory))
                System.IO.Directory.CreateDirectory(DownloadDirectory);

            appFinder1.Start();
            appFinder2.Start();
            appFinder3.Start();
            appFinder4.Start();
        }

        public void Stop()
        {
            RavenDbInstallationPath = appFinder2.InstalledPath;
            //appFinder1.Start();
            //appFinder2.Start();
            //appFinder3.Start();
        }
    }
}
